﻿namespace sfcms.api
{
    using System;
    using System.Data;

    public class folder
    {
        public string displayname;
        public static folder fileroot;
        public long id;
        public string name;
        public static folder pageroot = new folder();
        public long parentid;
        public int type;

        static folder()
        {
            pageroot.name = "Page Root";
            pageroot.displayname = "Page Root";
            pageroot.type = 1;
            fileroot = new folder();
            fileroot.name = "File Root";
            fileroot.displayname = "File Root";
            fileroot.type = 2;
        }

        public static folder byid(long id)
        {
            DataTable children = new database().ExecuteDataTable("SELECT * FROM SFCMS_Tree (NOLOCK) WHERE [ID] = @0", new object[] { "@0", id });
            if (children.Rows.Count == 0)
            {
                return null;
            }
            return fromdbrow(children.Rows[0]);
        }

        public static folder[] byparentid(long parentid, int type)
        {
            DataTable children = new database().ExecuteDataTable("SELECT * FROM SFCMS_Tree (NOLOCK) WHERE Parent = @0 AND [Type] = @1 ORDER BY [Name]", new object[] { "@0", parentid, "@1", type });
            folder[] childfolders = new folder[children.Rows.Count];
            for (int i = 0; i < childfolders.Length; i++)
            {
                childfolders[i] = fromdbrow(children.Rows[i]);
            }
            return childfolders;
        }

        public static folder byparentidanddisplayname(int type, long parentid, string displayname)
        {
            folder[] folders = byparentid(parentid, type);
            foreach (folder subfolder in folders)
            {
                if (urlrewriter.toasciilower(subfolder.displayname) == urlrewriter.toasciilower(displayname))
                {
                    return subfolder;
                }
            }
            return null;
        }

        private static folder fromdbrow(DataRow child)
        {
            return new folder { id = Convert.ToInt64(child["ID"]), name = child["Name"].ToString(), displayname = child["DisplayName"].ToString(), type = Convert.ToInt32(child["Type"]), parentid = Convert.ToInt64(child["Parent"]) };
        }

        public folder getparent()
        {
            if (this.parentid == 0L)
            {
                return ((this.type == pageroot.type) ? pageroot : fileroot);
            }
            return byid(this.parentid);
        }

        public void save()
        {
            if (this.id == 0L)
            {
                this.id = Convert.ToInt64(new database().ExecuteScalar("INSERT INTO SFCMS_Tree ([Name], [DisplayName], [Parent], [Type]) VALUES (@0, @1, @2, @3)", new object[] { "@0", this.name, "@1", this.displayname, database.CreateTypedParameterName("@2", DbType.Int64), this.parentid, database.CreateTypedParameterName("@3", DbType.Int32), this.type }));
            }
            else
            {
                new database().ExecuteNonQuery("UPDATE SFCMS_Tree SET [Name] = @0, [DisplayName] = @1 WHERE [ID] = @2", new object[] { "@0", this.name, "@1", this.displayname, "@2", this.id });
            }
        }
    }
}

